/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.auth.ui;

import java.util.List;
import javax.swing.JTable;
import org.springframework.context.ApplicationEvent;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.table.support.AbstractObjectTable;
import reportcenter.auth.business.RolesDao;
import reportcenter.auth.business.UserAuthorizationDao;
import reportcenter.auth.dominio.Roles;
import reportcenter.auth.dominio.Users;

/**
 *
 * @author renan
 */
public class RoleTable extends AbstractObjectTable {

    private RolesDao dao;

    public RoleTable(RolesDao dao) {
//        super("dataSources", new String[]{"Nome da Conexão","Driver JDBC","Usuário do Schema"});
        super("roles", new String[]{"roleName"});
        this.dao = dao;
    }
    
    public void refreshTable(){
        List list = getBaseEventList();
        list.clear();
        list.addAll(dao.getAll());
    }

    @Override
    protected Object[] getDefaultInitialData() {
        return dao.getAll().toArray();
    }

    protected void configureTable(JTable table) {
//        TableColumnModel tcm = table.getColumnModel();
//        //NAME, DRIVER, USU
//        tcm.getColumn(0).setPreferredWidth(80);
//        tcm.getColumn(1).setPreferredWidth(120);
//        tcm.getColumn(2).setPreferredWidth(50);
    }

    public Roles[] getSelectedRoles() {
        int[] selected = getTable().getSelectedRows();
        Roles[] roles = new Roles[selected.length];
        for (int i = 0; i < selected.length; i++) {
            roles[i] = (Roles) getTableModel().getElementAt(selected[i]);
        }
        return roles;
    }

    public Roles getSelectedRole() {
        return (Roles) getSelectedRoles()[0];
    }

    @Override
    protected boolean shouldHandleEvent(ApplicationEvent event) {
        if (event instanceof LifecycleApplicationEvent) {
            Class<? extends Object> eventClass = (((LifecycleApplicationEvent) event).getObject().getClass());
            return getTableObjectsClass().isAssignableFrom(eventClass) && super.shouldHandleEvent(event);
        } else {
            return super.shouldHandleEvent(event);
        }
    }

    public Class<? extends Object> getTableObjectsClass() {
        return Roles.class;
    }
}